﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FinosLibrary;
using System.Collections.ObjectModel;
using System.Windows;
using System.IO;
namespace FinosAdmin.Logics {
    [MessageProcess]
    public class Server:ModelLogic {
        public ObservableCollection<string> serverList { get; set; }
        private string ServerListPath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "ServerList.ini";
        private bool _EnableLog;
        public bool EnableLog {
            get { return _EnableLog; }
            set {
                _EnableLog = value;
                call("LogEnable", value);
            }
        }
        public string serverLog { get; set; }
        public string connectLabel { get; set; }
        public Server()
            : base() {
            connectLabel = "连接";
            if (!File.Exists(ServerListPath)) {
                File.WriteAllLines(ServerListPath, new string[] { "localhost" });
            }
            serverList = new ObservableCollection<string>(File.ReadAllLines(ServerListPath));
        }
        [MessageMethod]
        public override void NetConnection_Connected() {
            base.NetConnection_Connected();
            connectLabel = "断开";
            this.NotifyPropertyChanged(x => x.connectLabel);
        }
        [MessageMethod]
        public override void NetConnection_Disconnected(string reason) {
            base.NetConnection_Disconnected(reason);
            connectLabel = "连接";
            this.NotifyPropertyChanged(x => x.connectLabel);
        }
        [MessageMethod]
        public void ServiceInfo(string msg) {
            MessageBox.Show(msg,"系统消息");
        }
        [MessageMethod]
        public void ServerLog(string msg) {
            serverLog += msg+"\n";
            this.NotifyPropertyChanged(x => x.serverLog);
        }
        [MessageMethod]
        public void AddServerAddress(string newAddr) {
            serverList.Add(newAddr);
            File.WriteAllLines(ServerListPath, serverList.ToArray());
        }
        [MessageMethod]
        public void DeleteServerAddress(int addr) {
            serverList.RemoveAt(addr);
            File.WriteAllLines(ServerListPath, serverList.ToArray());
        }
    }
}
